import XEUtils from "xe-utils";

export const GetPrintFormat = function (item,Template,MT,DT,history){
    console.log(DT)
    let  t=0
    if(item===null){
        item={id:0,type:1,templateId:Template.templateID,name:'打印模板名称',content:{"style":{"padding":"50px 20px","width":"1160px"},"header":[],"detail":[],"title":"","extend":[],"footer":[],
                "base":{"style":{"paddingTop":"20px","paddingBottom":"60px","paddingLeft":"20px","paddingRight":"20px"},"iscard":1,"fy":1,"isHead":false,"isBody":false,
                   "cardGrid":24,"cardInterval":2,"height":50,"tableNames":MT.tableName,"fontSize":20,"pageSize":1160,"isFooter":false,"titleWidth":150}}
        }
    }else if (item===-1) {
        t=2
        item={id:0,type:1,templateId:Template.templateID,name:'打印模板名称',content:{"style":{"padding":"50px 20px","width":"1160px"},"header":[],"detail":[],"title":"","extend":[],"footer":[],
                "base":{"style":{"padding":"60px"},"iscard":1,"fy":1,"isHead":true,"isBody":true,
                    "cardGrid":24,"cardInterval":2,"height":50,"tableNames":MT.tableName,"fontSize":20,"pageSize":1160,"isFooter":false,"titleWidth":150}}
        }
        item.content.header.push({text:Template.templateTitle,type:0,style:{fontSize:"40px","text-align":"center"},"span":24})
       const TMF= XEUtils.find(MT.Fields,f=>f.componentType===5),fieldex=[]
        if(TMF!==null){
            fieldex.push(TMF.fieldName)
            item.content.header.push({
                "type": 8,
                "fieldName": TMF.fieldName,
                "span": 24,
                "style": {textAlign: "left",display:'block',position:'absolute',top:'-10px'},
                "height": 240
            })

        }

        XEUtils.arrayEach(MT.Fields,Field=>{
            if(Field.hidden===0&&Field.nullToField===0&&!fieldex.includes(Field.fieldName)){
                const f={fieldName:Field.fieldName,"type":1,title:Field.fieldDispName,"style":{"font-size":"20px"},"span":8}
                item.content.header.push(f)
            }
        })
        if(DT.length>0){
            item.content.base.tableNames=DT[0].tableName
            XEUtils.arrayEach(DT[0].Fields,Field=>{
                if(Field.hidden===0 && Field.nullToField ===0){
                    const f={fieldName:Field.fieldName,"type":1,title:Field.fieldDispName, "style":{"font-size":"20px"}}
                    item.content.detail.push(f)
                }
            })
        }
        if(history.length>0){
            item.content.base.isFooter=true
            XEUtils.lastArrayEach(history,it=>{
                if(!['开始','结束'].includes(it.taskName)){
                    item.content.footer.push({title:it.taskName,text:  XEUtils.toArray(it.desc).join("\n")  ,type:0,style:{fontSize:"20px"},"span":12})

                }
            })
            if(item.content.footer.length%2===1){
                item.content.footer.push({title:'',text:'' ,type:0,style:{fontSize:"20px"},"span":12})
            }
        }

    }else {
        item.content=XEUtils.isString(item.content)?JSON.parse(item.content):item.content
    }
    return {item ,t}
}
